package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Admin;
import com.kecheng.util.FileUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * 后台用户模块
 *
 * @author
 */

@Action(value = "user", results = {
        @Result(name = "userList", location = "/admin/user/userList.jsp"),
        @Result(name = "toDetail", location = "/admin/user/detail.jsp"),
        @Result(name = "redirectList", type = "redirect",location = "user!userList.action"),
})
@AllowedMethods(value = {("regex:.*")})
public class UserAction extends BaseAction {

    @Autowired
    CommonDao dao;

    private Admin user;
    private File file;
    private String fileFileName;
    private List<Integer> id;

    private PageBean<Admin> pageBean;


    /**
     * 用户列表
     * @return
     */
    public String userList(){
        pageBean = (PageBean<Admin>) dao.findPageBean(Admin.class,pageBean);
        return "userList";
    }
    /**
     *个人详情页
     * @return
     */
    public String toDetail(){
        if(user!=null)user = (Admin)dao.findOne(user);
        return "toDetail";
    }

    /**
     * 更新或者添加
     */
    @Transactional(readOnly = false)
    public String save(){

        if(user.getId()!=null){
            if(file!=null)user.setImg(FileUtil.pictureUtil(file,fileFileName,root));
            dao.update(user);
        }else{
            user.setNum(0);
            user.setLasttime("");
            user.setThistime("");
            user.setImg(FileUtil.pictureUtil(file,fileFileName,root));
            dao.add(user);
        }

        return "redirectList";
    }

    /**
     *删除
     * @return
     */
    @Transactional(readOnly = false)
    public String delete(){
        if(user!=null)dao.delete(user);
        else if(id!=null){
            for(Integer i:id){
                user = new Admin();
                user.setId(i);
                dao.delete(user);
            }
        }
        return "redirectList";
    }

    public PageBean<Admin> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<Admin> pageBean) {
        this.pageBean = pageBean;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public Admin getUser() {
        return user;
    }

    public void setUser(Admin user) {
        this.user = user;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }
}
